package com.tangkun.environment.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.tangkun.environment.R;
import com.tangkun.environment.adapter.YiChangShuJuTiJiaoAdapter;
import com.tangkun.environment.entity.gongzuo.ReportIndexReturn;
import com.tangkun.environment.network.MySubscribe;
import com.tangkun.environment.rx.RxActivityHelper;
import com.tangkun.environment.utils.JumpHelper;
import com.tangkun.environment.view.CustomListView;

import org.simple.eventbus.EventBus;
import org.simple.eventbus.Subscriber;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 异常数据提交 列表
 */

public class YiChangShuJuTiJaoActivity extends BaseActivity implements CustomListView.OnLoadMoreListener, CustomListView.OnRefreshListener {

    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.next)
    TextView next;
    @BindView(R.id.custom_list_view)
    CustomListView customListView;
    private Context mContext;
    /**
     * 用户ID
     */
    private String id = "";

    public int page = 1;// 当前页
    private int totalCount = 0;// 总数量
    private boolean OnHeadRefreshTAG = true;// 为true时清楚已有数据

    /**
     * 数据源
     */
    private List<ReportIndexReturn.DataBean.ListBean> dataList = new ArrayList<ReportIndexReturn.DataBean.ListBean>();
    /**
     * 适配器
     */
    private YiChangShuJuTiJiaoAdapter adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        id = getIntent().getStringExtra("id");
        setContentView(R.layout.activity_common_customlistview);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        initView();
        title.setText("异常数据提交");
        next.setText("创建");
        reportIndex();
    }

    private void initView() {
        customListView.setOnRefreshListener(this);
        customListView.setOnLoadListener(this);
        customListView.setCanLoadMore(true);
        customListView.setAutoLoadMore(true);
        adapter = new YiChangShuJuTiJiaoAdapter(YiChangShuJuTiJaoActivity.this, dataList);
        customListView.setAdapter(adapter);
    }

    @OnClick({R.id.back, R.id.next})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                JumpHelper.finish(mContext);
                break;
            case R.id.next://创建
                Intent intent = new Intent(YiChangShuJuTiJaoActivity.this, JuBaoYiChangActivity.class);
                intent.putExtra("id", id);
                JumpHelper.jump(YiChangShuJuTiJaoActivity.this, intent, false);
                break;
        }
    }

    @Override
    public void onRefresh() {
        page = 1;
        OnHeadRefreshTAG = true;
        customListView.setCanLoadMore(true);
        reportIndex();
    }

    @Override
    public void onLoadMore() {
        OnHeadRefreshTAG = false;
        if (page * pageSize < totalCount) {
            page++;
            reportIndex();
        } else {
            customListView.onLoadMoreCompleteNoData();
        }
    }

    /**
     * 异常数据提交 列表
     */
    private void reportIndex() {
        Map<String, String> map = new HashMap<>();
        map.put("pageNumber", page + "");
        map.put("pageSize", pageSize + "");
        fromNetwork.reportIndex(map).compose(new RxActivityHelper<ReportIndexReturn>().ioMain(YiChangShuJuTiJaoActivity.this, true))
                .subscribe(new MySubscribe<ReportIndexReturn>(mContext, true) {
                    @Override
                    protected void success(ReportIndexReturn resultBean) {
                        if (resultBean.data != null) {
                            customListView.onLoadMoreComplete();
                            customListView.onRefreshComplete();
                            customListView.setCanLoadMore(true);
                            totalCount = resultBean.data.totalRow;
                            if (page * pageSize >= totalCount) {
                                customListView.setCanLoadMore(false);
                            }
                            //下拉刷新
                            if (OnHeadRefreshTAG) {
                                OnHeadRefreshTAG = false;
                                dataList.clear();
                            }
                            //填充数据
                            dataList.addAll(resultBean.data.list);
                            //刷新列表
                            adapter.notifyDataSetChanged();
                        }
                    }

                    @Override
                    protected void onMyError(String errorMessage) {
                        showToast(errorMessage);
                    }
                });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    //创建咨询刷新
    @Subscriber(tag = "YiChangShuJuTiJaoActivity.refresh")
    private void refresh(boolean flag) {
        onRefresh();
    }
}
